package com.ruoyi.different.scheduled;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.different.domain.DifferentRete;
import com.ruoyi.different.service.IDifferentReteService;
import com.ruoyi.different.service.impl.WeixinFsfunction;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;


@Component
public class dataScheduled {

    @Autowired
    private IDifferentReteService differentReteService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private WeixinFsfunction weixinFsfunction;


    @Scheduled(cron = "0 0 7 ? * MON")
    public void sendMessage() {
        /*
        * 30天到90天，每周1次
        * */
       //查出90到30天内的数据
        //怎么查，当前时间减去90天，当前时间减去30天
        //发送时间每周一 7点
        Date date = new Date();
        DateTime dateTime = DateUtil.offsetDay(date, 90);
        DateTime dateTime1 = DateUtil.offsetDay(date, 30);
        QueryWrapper<DifferentRete> queryWrapper = new QueryWrapper<>();
       queryWrapper.lambda().between(DifferentRete::getMaturityTime,dateTime1,dateTime);
        List<DifferentRete> list = differentReteService.list(queryWrapper);
        for (DifferentRete differentRete : list) {
            //未转化
            if(Objects.nonNull(differentRete.getConversion())&&differentRete.getConversion()==4002){
                //存在公众号id
             /*   SysUser user2 = new SysUser();
                user2.setUserId((long)differentRete.getEntryClerk());
                List<SysUser> sysUsers1 = sysUserService.selectUserListOpen(user2);*/
                QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
                sysUserQueryWrapper.lambda().eq(SysUser::getUserId,differentRete.getEntryClerk());
                List<SysUser> sysUsers1 = sysUserService.list(sysUserQueryWrapper);
                if (Objects.nonNull(sysUsers1)&&sysUsers1.size()>0&&Objects.nonNull(sysUsers1.get(0).getGzhopenId())){
                    String gzhopenId = sysUsers1.get(0).getGzhopenId();
                    TreeMap<String, String> miniprograms = new TreeMap<String, String>();
                    miniprograms.put("appid", "wx19b86a5fba0825ce");
                    miniprograms.put("pagepath", "/pages/detail/detail?id="+differentRete.getId());
                    ArrayList<String> strings = new ArrayList<>();
                    strings.add("thing2");
                    strings.add("phone_number8");
                    strings.add("time9");

                    ArrayList<String> strings1 = new ArrayList<>();
                    String thing2=differentRete.getName();
                    if (differentRete.getName().length()>3){
                        thing2=differentRete.getName().substring(0,3)+"..";
                    }
                    strings1.add(thing2+DateUtil.formatDate(differentRete.getMaturityTime())+"商机到期");
                    strings1.add(differentRete.getTelephone());
                    strings1.add(DateUtil.formatDate(new Date()));

                    weixinFsfunction.sendWarningByWechat(gzhopenId, miniprograms, strings, strings1, "CVNdfAFZdbbgkNIiXC6QmxoAcCJvcksLGEa21nbHLfQ");

                }
            }
        }
    }
    // 30天之内，每周2次
    //当前时间减去30天
    //发送时间每周一  早上8点
    //二次发送  每周四 早上8点
    @Scheduled(cron = "0 0 8 ? * MON")
    public void sendMessagethA() {
        /*
         * 30天到90天，每周1次
         * */
        //查出90到30天内的数据
        //怎么查，当前时间减去90天，当前时间减去30天
        //发送时间每周一 7点
        Date date = new Date();
        //DateTime dateTime = DateUtil.offsetDay(date, 90);
        DateTime dateTime1 = DateUtil.offsetDay(date, 30);
        QueryWrapper<DifferentRete> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().between(DifferentRete::getMaturityTime,date,dateTime1);
        List<DifferentRete> list = differentReteService.list(queryWrapper);
        for (DifferentRete differentRete : list) {
            //未转化
            if(Objects.nonNull(differentRete.getConversion())&&differentRete.getConversion()==4002){
                //存在公众号id
             /*   SysUser user2 = new SysUser();
                user2.setUserId((long)differentRete.getEntryClerk());
                List<SysUser> sysUsers1 = sysUserService.selectUserListOpen(user2);*/
                QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
                sysUserQueryWrapper.lambda().eq(SysUser::getUserId,differentRete.getEntryClerk());
                List<SysUser> sysUsers1 = sysUserService.list(sysUserQueryWrapper);
                if (Objects.nonNull(sysUsers1)&&sysUsers1.size()>0&&Objects.nonNull(sysUsers1.get(0).getGzhopenId())){
                    String gzhopenId = sysUsers1.get(0).getGzhopenId();
                    TreeMap<String, String> miniprograms = new TreeMap<String, String>();
                    miniprograms.put("appid", "wx19b86a5fba0825ce");
                    miniprograms.put("pagepath", "/pages/detail/detail?id="+differentRete.getId());
                    ArrayList<String> strings = new ArrayList<>();
                    strings.add("thing2");
                    strings.add("phone_number8");
                    strings.add("time9");

                    ArrayList<String> strings1 = new ArrayList<>();
                    String thing2=differentRete.getName();
                    if (differentRete.getName().length()>3){
                        thing2=differentRete.getName().substring(0,3)+"..";
                    }
                    strings1.add(thing2+DateUtil.formatDate(differentRete.getMaturityTime())+"商机到期");
                    strings1.add(differentRete.getTelephone());
                    strings1.add(DateUtil.formatDate(new Date()));
                    weixinFsfunction.sendWarningByWechat(gzhopenId, miniprograms, strings, strings1, "CVNdfAFZdbbgkNIiXC6QmxoAcCJvcksLGEa21nbHLfQ");
                }
            }
        }
    }


    @Scheduled(cron = "0 0 8 ? * THU")
    public void sendMessagethB() {
        /*
         * 30天到90天，每周1次
         * */
        //查出90到30天内的数据
        //怎么查，当前时间减去90天，当前时间减去30天
        //发送时间每周一 7点
        Date date = new Date();
        //DateTime dateTime = DateUtil.offsetDay(date, 90);
        DateTime dateTime1 = DateUtil.offsetDay(date, 30);
        QueryWrapper<DifferentRete> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().between(DifferentRete::getMaturityTime,date,dateTime1);
        List<DifferentRete> list = differentReteService.list(queryWrapper);
        for (DifferentRete differentRete : list) {
            //未转化
            if(Objects.nonNull(differentRete.getConversion())&&differentRete.getConversion()==4002){
                //存在公众号id
             /*   SysUser user2 = new SysUser();
                user2.setUserId((long)differentRete.getEntryClerk());
                List<SysUser> sysUsers1 = sysUserService.selectUserListOpen(user2);*/
                QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
                sysUserQueryWrapper.lambda().eq(SysUser::getUserId,differentRete.getEntryClerk());
                List<SysUser> sysUsers1 = sysUserService.list(sysUserQueryWrapper);
                if (Objects.nonNull(sysUsers1)&&sysUsers1.size()>0&&Objects.nonNull(sysUsers1.get(0).getGzhopenId())){
                    String gzhopenId = sysUsers1.get(0).getGzhopenId();
                    TreeMap<String, String> miniprograms = new TreeMap<String, String>();
                    miniprograms.put("appid", "wx19b86a5fba0825ce");
                    miniprograms.put("pagepath", "/pages/detail/detail?id="+differentRete.getId());
                    ArrayList<String> strings = new ArrayList<>();
                    strings.add("thing2");
                    strings.add("phone_number8");
                    strings.add("time9");

                    ArrayList<String> strings1 = new ArrayList<>();
                    String thing2=differentRete.getName();
                    if (differentRete.getName().length()>3){
                        thing2=differentRete.getName().substring(0,3)+"..";
                    }
                    strings1.add(thing2+DateUtil.formatDate(differentRete.getMaturityTime())+"商机到期");
                    strings1.add(differentRete.getTelephone());
                    strings1.add(DateUtil.formatDate(new Date()));
                    weixinFsfunction.sendWarningByWechat(gzhopenId, miniprograms, strings, strings1, "CVNdfAFZdbbgkNIiXC6QmxoAcCJvcksLGEa21nbHLfQ");
                }
            }
        }
    }
}
